package com.yd.app.mapper.provider;

import com.alibaba.druid.util.StringUtils;
import com.yd.domain.searchParam.MessageGroupSearchParam;

import java.util.Map;

/**
 * Created by Administrator on 2018/6/5.
 */
public class MessageGroupProvider extends BaseProvider {
    public String getMessageGroupList(Map map){
        MessageGroupSearchParam searchParam = (MessageGroupSearchParam)map.get("model");
        StringBuffer buffer = new StringBuffer();
        buffer.append("SELECT bmg.id,bmg.title,bmg.content,bmg.inputUser,bmg.inputDate,bmg.state,bmg.imgUrl,(SELECT realName FROM sys_user WHERE id = bmg.inputUser) AS inputUserName FROM business_message_group bmg");
        //添加条件
        buffer.append(this.buildSearchUserSqlConditions(searchParam));
        addFenyeSql(buffer);
        return buffer.toString();
    }

    private String buildSearchUserSqlConditions(MessageGroupSearchParam param){
        StringBuilder buffer = new StringBuilder(" where 1=1");
        if(!StringUtils.isEmpty(param.getContent())){
            buffer.append(" AND bmg.content like '%"+param.getContent()+"%'");
        }
        if(!StringUtils.isEmpty(param.getTitle())){
            buffer.append(" AND bmg.title like '%"+param.getTitle()+"%'");
        }
        buffer.append(" order by bmg.inputDate desc ");
        return buffer.toString();
    }
    /////////////////////////////////////以下是手机端查询/////////////////////////////////////////////////
    public String getCusQFMsgList(Map map){
        MessageGroupSearchParam searchParam = (MessageGroupSearchParam)map.get("param");
        StringBuilder sb = new StringBuilder();
        sb.append("select u.*,g.*,(SELECT realName FROM sys_user WHERE id = g.inputUser) as inputUserName from business_message_group_user u LEFT JOIN business_message_group g " +
                "on u.messageGroupId=g.id where 1=1 ");
        //添加条件
        sb.append(this.getCusQFMsgConditions(searchParam));
        sb.append(" order by g.inputDate desc ");
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    public String getCusQFMsgCount(Map map){
        MessageGroupSearchParam searchParam = (MessageGroupSearchParam)map.get("param");
        StringBuilder sb=new StringBuilder("SELECT COUNT(1)  from business_message_group_user u LEFT JOIN business_message_group g " +
                "on u.messageGroupId=g.id where 1=1  ");
        sb.append(getCusQFMsgConditions(searchParam));
        return sb.toString();
    }
    private String getCusQFMsgConditions(MessageGroupSearchParam param){
        StringBuilder sb = new StringBuilder();
        if(!StringUtils.isEmpty(param.getUserId())){
            sb.append(" AND u.userId = '"+param.getUserId()+"' ");
        }
        if(!StringUtils.isEmpty(param.getState())){
            sb.append(" AND u.state = '"+param.getState()+"' ");
        }
        sb.append(" AND u.isShow = '0' ");

        return sb.toString();
    }
}
